-- Copyright 2002-2008.  Adobe Systems, Incorporated.  All rights reserved.
-- Calculate the geometric center of all text art of kind paragraph text.
-- To use, create a document with one or more text layers whose
-- type is paragraph text

tell application "Adobe Photoshop CS4"
	activate
	
	if ((count documents) < 1) then
		display dialog "Create a document with one or more text layers whose type is paragraph text."
		return
	end if
	
	set docRef to current document
	set artLayerList to get every art layer of docRef whose kind is text layer
	set itemBounds to {0, 0, 0, 0}
	set paragraphTextFound to false
	
	if (length of artLayerList > 0) then
		
		repeat with layerRef in artLayerList
			set current layer of docRef to layerRef
			set textItemRef to text object of layerRef
			if (kind of textItemRef is paragraph text) then
				set paragraphTextFound to true
				set itemPosition to position of textItemRef
				set itemBounds to {item 1 of itemPosition, item 2 of itemPosition 
					, (item 1 of itemPosition) + (width of textItemRef), (item 2 of itemPosition) + (height of textItemRef)}
			end if
		end repeat
		if (paragraphTextFound) then
			set itemCenter to GetItemCenter(itemBounds) of me
			display dialog "Center of Paragraph Text  x: " & item 1 of itemCenter & " ,   y: " & item 2 of itemCenter
		else
			display dialog "Script requires a document with at least one paragraph-type text layer"
		end if
		
	else
		display dialog "Script requires a document with at least one paragraph-type text layer"
	end if
end tell

-- This handler finds the center of an item given its bounds
on GetItemCenter(itemBounds)
	-- Assign coordinates from the bounds to individual variables
	set {itemLeft, itemTop, itemRight, itemBottom} to itemBounds
	-- Calculate the center position
	set xCenter to (itemLeft + itemRight) / 2
	set yCenter to (itemTop + itemBottom) / 2
	return {xCenter, yCenter}
end GetItemCenter

